ShouXunScene={}
ShouXunSceneFunc={}
ShouXunSceneFunc.__index = ShouXunSceneFunc
setmetatable(ShouXunScene, ShouXunSceneFunc)

ShouXunSceneFunc.getNode = function()
  local l = {}
  setmetatable(l, ShouXunSceneFunc)
  l.node = CCBReader:load("ShouXunScene.ccbi")
  print("ShouXunScene.getNode", l.node)
  ShouXunScene.instance = l
  l:init() 
  return l 
end

ShouXunSceneFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(ShouXunScene.getNode().node)
  print("ShouXunScene.getScene", sc)
  return sc
end

ShouXunSceneFunc.init = function(self)
  b = self.node:getChildByTag(1111)
  setObjectExtTable(b, self)
  setObjectExtTable(self.node:getChildByTag(1021), self)
  setObjectExtTable(self.node:getChildByTag(1022), self)
  setObjectExtTable(self.node:getChildByTag(1023), self)
  setObjectExtTable(self.node:getChildByTag(1024), self)
  setObjectExtTable(self.node:getChildByTag(1025), self)
  
  setObjectExtTable(self.node:getChildByTag(1026), self)
  setObjectExtTable(self.node:getChildByTag(1027), self)
  setObjectExtTable(self.node:getChildByTag(1028), self)
  
  local layer = self.node:getChildByTag(1001)
  setTouchEnabledAll(layer, false);
  layer:setVisible(false)

  self:setTable()
  self:setButtonState()
  self:setLabelText()
end

ShouXunSceneFunc.setFocus = function(self, node)
  if self.focusSprite~=nil then
    --node:getParent():addChild(self.focusSprite)  
    self.focusSprite:getParent():removeChild(self.focusSprite, true)
  end
  self.focusSprite = CCSprite:create("itemfocus.png")
  self.focusSprite:setPosition(node:getPosition())
  node:getParent():addChild(self.focusSprite)
end
ShouXunSceneFunc.setLabelText = function(self)
  TypeCast(self.node:getChildByTag(1011), "CCLabelTTF"):setString(tostring(UserData.userinfo.host.coin))
  TypeCast(self.node:getChildByTag(1012), "CCLabelTTF"):setString(tostring(UserData.userinfo.host.cash))
end
ShouXunSceneFunc.setButtonState = function(self)
  for i=1,5 do
    TypeCast(self.node:getChildByTag(1020+i), "CCButton"):setEnabled(false)
  end
  for i=2,5 do
    TypeCast(self.node:getChildByTag(1030+i), "CCButton"):setVisible(false)
  end

  local decorate = UserData.userinfo.host.store.decor
  TypeCast(self.node:getChildByTag(1021), "CCButton"):setEnabled(true)
  for k,v in pairs(decorate.n) do
    TypeCast(self.node:getChildByTag(1020+k), "CCButton"):setEnabled(true)
    TypeCast(self.node:getChildByTag(1030+k), "CCSprite"):setVisible(true)
    if k==4 then
      TypeCast(self.node:getChildByTag(1029), "CCButton"):setVisible(false)
    end
  end
end

ShouXunSceneFunc.setTable = function(self)
  self.focusSprite = nil
  local decorate = UserData.userinfo.host.store.decor
  local tc = 0
  for k,v in pairs(decorate.m) do tc=tc+1 end
  tc = math.floor((tc-1)/2)+1  
  if tc<5 then tc=5 end
  local items = CCArray:create()
  for i=1,tc do
    local item = CCLayer:create()
    addSprite(item, ccp(45, 140), "itembk.png"):setTag(0) 
    addSprite(item, ccp(45, 45), "itembk.png"):setTag(1)
    item:setContentSize(CCSizeMake(90, 180))
    items:addObject(item)
  end  

  local count = 0 
  for k,v in pairs(decorate.m) do
    local info = UserData.config.models.Decorate[v.d]
    local index = math.floor(count/2)
    local index1 = count%2
    local item = items:objectAtIndex(index)
    local b = addButtonFromBK(item, index1, nameFromPath(info.asset), "", "onClickDecorate", self)
    b:setTag(tonumber(k))
    count=count+1
  end
  if table~=nil then
    self.node:removeChild(self.table, true)
  end
  local tableview = CCTableView:create(0, CCSizeMake(539, 199), items)
  tableview:setPosition(ccp(50, 295)) 
  self.table = tableview
  self.node:addChild(tableview)
end
ShouXunSceneFunc.setDecorateInfo = function(self, index)
  local layer = self.node:getChildByTag(1001)
  setTouchEnabledAll(layer, true);
  layer:setVisible(true)

  setObjectExtTable(layer:getChildByTag(1020), self)
  setObjectExtTable(layer:getChildByTag(1021), self)
  
  local d = Decorate:create(index) 
  local l, xp1, xp2 = d:calcLevel()
  TypeCast(layer:getChildByTag(1010), "CCLabelTTF"):setString(string.format("%s Lv%d", d.itemConfig.name, l))
  TypeCast(layer:getChildByTag(1010), "CCLabelTTF"):setColor(Equip.EquipColor[d.itemConfig.quality])
  TypeCast(layer:getChildByTag(1011), "CCLabelTTF"):setString(string.format("%d/%d", xp1, xp2))

  if d.itemConfig.quality ~="gray" then  
    local tmp = string.format("level%d", l)
    local boost = d.itemConfig[tmp]
    local desc = Equip.EquipAttrDesc[d.itemConfig.effect]
    TypeCast(layer:getChildByTag(1012), "CCLabelTTF"):setString(string.format("%s +%d", desc, boost))
    layer:getChildByTag(1020):setVisible(true)
  else
    layer:getChildByTag(1020):setVisible(false)
    TypeCast(layer:getChildByTag(1012), "CCLabelTTF"):setString("")
  end

  TypeCast(layer:getChildByTag(1013), "CCLabelTTF"):setString(string.format("%d", d.itemConfig.saleCash))
end

ShouXunSceneFunc.onClickDecorate = function(self, node, event)
  local layer = self.node:getChildByTag(1001)
  local index = node:getTag()
  self.currentIndex = index
  self:setDecorateInfo(index)
  self:setFocus(node)
end

ShouXunSceneFunc.onGetButton = function(self, node, event)
  NetworkRequest.pickUp(self.currentIndex, "onGetComplete", self)
  --setTouchEnabledAll(self.node, false)
  LoadScene:showLoadingScene()
end
ShouXunSceneFunc.onSellButton = function(self, node, event)
  NetworkRequest.sold(self.currentIndex, "onGetComplete", self)
  --setTouchEnabledAll(self.node, false)
  LoadScene:showLoadingScene()
end
ShouXunSceneFunc.onGetComplete = function(self, data)
  --setTouchEnabledAll(self.node, true)
  LoadScene:removeLoadingScene()
  self:setButtonState()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    self:setTable()
    self:setLabelText()
  else
    showMessage(res.msg, ccc3(255,255,0))
  end  
end
--ShouXunSceneFunc.onSellComplete = function(self, data)
--end

ShouXunSceneFunc.onGetXunZhangButton = function(self, node, event)
  local index = node:getTag() - 1020
  NetworkRequest.decorate(index, "onDecorateComplete", self)
  --setTouchEnabledAll(self.node, false)
  LoadScene:showLoadingScene() 
end
ShouXunSceneFunc.onDecorateComplete = function(self, data)
  --setTouchEnabledAll(self.node, true)
  LoadScene:removeLoadingScene()
  self:setButtonState()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    self:setTable()
    self:setLabelText()
  else
    showMessage(res.msg, ccc3(255,255,0))
  end
end

ShouXunSceneFunc.onXunZhangBagButton = function(self, node, event)
  --setTouchEnabledAll(self.node, false)
  --local l = ShouXunScene1:getNode().node--CCBReader:load("ShouXunScene1.ccbi")
  --self.node:getParent():addChild(l)
  --self.node:getParent():removeChild(self.node, true)
  --ShouXunScene.instance = nil
end
ShouXunSceneFunc.onShouXunAll = function(self, node, event)
end
ShouXunSceneFunc.onPickUpAll = function(self, node, event)
  NetworkRequest.pickUpAll("onPickUpAllComplete", self)
  --setTouchEnabledAll(self.node, false)
  LoadScene:showLoadingScene()
end
ShouXunSceneFunc.onPickUpAllComplete = function(self, data)
  print("ShouXunSceneFunc.onPickUpAllComplete", data)
  setTouchEnabledAll(self.node, true)
  --LoadScene:removeLoadingScene()
  self:setButtonState()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    self:setTable()
    self:setLabelText()
  else
    showMessage(res.msg, ccc3(255,0,0))
  end
end
ShouXunSceneFunc.onSoldAll = function(self, node, event)
  NetworkRequest.soldAll("onSoldAllComplete", self)
  --setTouchEnabledAll(self.node, false)
  LoadScene:showLoadingScene()
end
ShouXunSceneFunc.onSoldAllComplete = function(self, data)
  print("ShouXunSceneFunc.onSoldAllComplete", data)
  --setTouchEnabledAll(self.node, true)
  LoadScene:removeLoadingScene()
  self:setButtonState()
  local res = cjson.decode(data)
  if res.ret==0 and res.msg=="" then
    NetworkRequest.syncData(res)
    self:setTable()
    self:setLabelText()
  else
    showMessage(res.msg, ccc3(255,0,0))
  end
end

--ShouXunSceneFunc.onCloseBagWnd = function(self, node, event)
--  setTouchEnabledAll(self.node, false)
--  self.node:removeChild(node.getParent(), true) 
--end
ShouXunSceneFunc.onClose = function(self, node, event)
  setTouchEnabledAll(self.node:getParent(), true) 
  self.node:getParent():removeChild(self.node, true)
  ShouXunScene.instance = nil
  if ShouXunScene1.instance ~= nil then
    ShouXunScene1.instance:clear()
    ShouXunScene1.instance:setBagTable()
    ShouXunScene1.instance:setButtonState()
  end
--  MainScene.scene:removeChild(node:getParent(), true)
--  MainScene.setState()
--  setTouchEnabledAll(MainScene.node, true)
--  ShouXunScene.instance = nil
end
